<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.png">
  <link rel="mask-icon" href="/images/avatar.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"notes.maxwi.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Python可以使用pyenv管理并切换同一系统下的多个python解释器版本，pyenv支持非常多的python版本，也包括各大知名python的衍生版，如anaconda。而virtualenv可以为每个文件夹指定一个特定的python环境，各文件夹的python环境独立，共用一个统一的python基础版本，可以用于隔离不同项目的包环境，及包版本。当然pyenv也支持为不同的文件夹指定不同的p">
<meta property="og:type" content="article">
<meta property="og:title" content="Python多版本管理工具pyenv及环境隔离工具virtualenv">
<meta property="og:url" content="http://notes.maxwi.com/2017/03/06/python-pyenv-virtualenv/index.html">
<meta property="og:site_name" content="blueyi&#39;s notes">
<meta property="og:description" content="Python可以使用pyenv管理并切换同一系统下的多个python解释器版本，pyenv支持非常多的python版本，也包括各大知名python的衍生版，如anaconda。而virtualenv可以为每个文件夹指定一个特定的python环境，各文件夹的python环境独立，共用一个统一的python基础版本，可以用于隔离不同项目的包环境，及包版本。当然pyenv也支持为不同的文件夹指定不同的p">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2017-03-06T11:44:57.000Z">
<meta property="article:modified_time" content="2017-03-06T11:44:57.000Z">
<meta property="article:author" content="blueyi">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://notes.maxwi.com/2017/03/06/python-pyenv-virtualenv/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>Python多版本管理工具pyenv及环境隔离工具virtualenv | blueyi's notes</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="blueyi's notes" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="blueyi's notes" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">blueyi's notes</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Follow Excellence,Success will chase you!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://notes.maxwi.com/2017/03/06/python-pyenv-virtualenv/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/default_avatar.jpg">
      <meta itemprop="name" content="blueyi">
      <meta itemprop="description" content="心怀善意，虛怀若谷！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="blueyi's notes">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Python多版本管理工具pyenv及环境隔离工具virtualenv
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2017-03-06 19:44:57" itemprop="dateCreated datePublished" datetime="2017-03-06T19:44:57+08:00">2017-03-06</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>4.7k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>4 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>Python可以使用pyenv管理并切换同一系统下的多个python解释器版本，pyenv支持非常多的python版本，也包括各大知名python的衍生版，如anaconda。而virtualenv可以为每个文件夹指定一个特定的python环境，各文件夹的python环境独立，共用一个统一的python基础版本，可以用于隔离不同项目的包环境，及包版本。当然pyenv也支持为不同的文件夹指定不同的python版本，但不同文件夹下的同一个python版本共用环境，所以pyenv与virtualenv结合使用，便可以实现任意版本下的环境隔离随意切换，pyenv自带virtualenv插件。<br>可以简单理解如下：</p>
<ul>
<li>pyenv 实现Python版本管理</li>
<li>virtualenv 实现Python环境隔离</li>
</ul>
<span id="more"></span>
<p>我的系统环境为Ubuntu16.04.2</p>
<h2 id="安装pyenv"><a href="#安装pyenv" class="headerlink" title="安装pyenv"></a>安装pyenv</h2><h3 id="安装依赖"><a href="#安装依赖" class="headerlink" title="安装依赖"></a>安装依赖</h3><p>其实pyenv是个脚本，并不需要安装它的依赖，但pyenv安装的python为通过下载相应的python源码，然后进行编译，所以需要安装相应的编译依赖：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \</span><br><span class="line">libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \</span><br><span class="line">xz-utils tk-dev</span><br></pre></td></tr></table></figure>

<h3 id="pyenv安装"><a href="#pyenv安装" class="headerlink" title="pyenv安装"></a>pyenv安装</h3><p>官方提供了自动安装脚本来安装pyenv，直接运行即可：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash</span><br></pre></td></tr></table></figure>
<p>pyenv会被安装在用户根目录的<code>~/.pyenv</code>路径，以后通过pyenv安装的所以python版本也都将在该目录中。上述命令会在用户的<code>.bashrc</code>中添加以下内容：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># pyenv</span><br><span class="line">export PYENV_ROOT=&quot;$HOME/.pyenv&quot;</span><br><span class="line">export PATH=&quot;$PYENV_ROOT/bin:$PATH&quot;</span><br><span class="line">eval &quot;$(pyenv init -)&quot;</span><br><span class="line">eval &quot;$(pyenv virtualenv-init -)&quot;</span><br></pre></td></tr></table></figure>

<p>卸载pyenv也只需要删除目录<code>~/.pyenv</code>以及相应的<code>.bashrc</code>中的内容即可</p>
<p>更新pyenv的命令为：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv update</span><br></pre></td></tr></table></figure>

<p>安装完成之后通过<code>pyenv --version</code>，查看其版本</p>
<h2 id="pyenv使用"><a href="#pyenv使用" class="headerlink" title="pyenv使用"></a>pyenv使用</h2><p>pyenv的核心命令，以及常用的核心命令用法如下：<br><strong>pyenv commands</strong><br>查看pyenv所支持的所有命令：</p>
<p><strong>pyenv version</strong><br>查看当前激活的python版本及相关信息<br>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv version</span><br><span class="line">3.6.1 (set by /home/blueyi/tpyenv/.python-version)</span><br></pre></td></tr></table></figure>

<p><strong>pyenv versions</strong><br>查看系统中的所有python版本，当前使用的版本使用*标记<br>输出如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv versions</span><br><span class="line">  system</span><br><span class="line">  2.7.13</span><br><span class="line">* 3.6.1 (set by /home/blueyi/tpyenv/.python-version)</span><br></pre></td></tr></table></figure>

<p><strong>pyenv which <command></strong><br>查看指定python的完全路径<br>如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv which python3</span><br><span class="line">/home/blueyi/.pyenv/versions/3.6.1/bin/python3</span><br></pre></td></tr></table></figure>
<p>注意command必须是shell命令</p>
<p><strong>pyenv whence [–path] <command></strong><br>列出包含给定命令的所有python版本<br>如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv whence pip</span><br><span class="line">2.7.13</span><br><span class="line">3.6.1</span><br></pre></td></tr></table></figure>

<p><strong>pyenv install</strong><br>安装指定版本的python<br>该命令后面可以跟一些参数，或者版本号，可以通过以下命令查看所有支持的python版本：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv install -l|--list</span><br></pre></td></tr></table></figure>
<p>安装python3.6.1：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv install 3.6.1 -v</span><br></pre></td></tr></table></figure>
<p>安装miniconda3-4.3.11：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv install miniconda3-4.3.11</span><br></pre></td></tr></table></figure>
<p>加上参数<code>-v</code>，用于显示详细安装过程<br>安装完成之后可以通过以下命令重建shims，以便让pyenv能够找到相应的版本及包，通过安装新版本的python以及包之后都应该运行一次：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv rehash</span><br></pre></td></tr></table></figure>
<p>安装完成之后可以通过上面的<code>pyenv versions</code>来查看系统中的所有python版本<br>安装的python在路径<code>~/.pyenv/versions/</code>中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ ls ~/.pyenv/versions/</span><br><span class="line">2.7.13  3.6.1</span><br></pre></td></tr></table></figure>

<p><strong>pyenv uninstall [-f|–force] <version></strong><br>卸载指定的版本</p>
<p><strong>pyenv local</strong><br>为当前目录设置指定的python版本，可以创建一个文件夹，在其中测试：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv local 3.6.1</span><br></pre></td></tr></table></figure>
<p>该命令会在当前文件夹下创建文件<code>.python-version</code>，并将相应的版本号写入其中，该版本号将覆盖全局版本。<br>如果local后面不跟版本号，则会显示当前局部版本信息</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv <span class="built_in">local</span></span></span><br><span class="line">3.6.1</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">cat</span> .python-version</span></span><br><span class="line">3.6.1</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python --version</span></span><br><span class="line">Python 3.6.1</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv versions</span></span><br><span class="line">  system</span><br><span class="line">  2.7.13</span><br><span class="line">* 3.6.1 (set by /home/blueyi/tpyenv/.python-version)</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv global</span></span><br><span class="line">system</span><br></pre></td></tr></table></figure>
<p>从上述命令的输出中可以看出，我的当前目录python版本被设置为了3.6.1，而全局的python版本使用的是系统自带的版本（2.7.12），同时我又安装了2.7.13版本。<br>取消当前的局部版本设置：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv local --unset</span><br></pre></td></tr></table></figure>
<p>local后面也可以接多个版本号，优先级按命令后面的顺序排列（摘自官方文档）：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv <span class="built_in">local</span> 2.7.6 3.3.3</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv versions</span></span><br><span class="line">  system</span><br><span class="line">* 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)</span><br><span class="line">* 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python --version</span></span><br><span class="line">Python 2.7.6</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python2.7 --version</span></span><br><span class="line">Python 2.7.6</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python3.3 --version</span></span><br><span class="line">Python 3.3.3</span><br></pre></td></tr></table></figure>

<p><strong>pyenv global</strong><br>与local命令类似，只是它用于设置全局的python版本，该版本号存储在文件<code>~/.pyenv/version</code>中，该版本会被局部版本号或者环境变量<code>PYENV_VERSION</code>指定的版本号覆盖。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python --version</span></span><br><span class="line">Python 2.7.12</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv global 2.7.13</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python --version</span></span><br><span class="line">Python 2.7.13</span><br></pre></td></tr></table></figure>
<p>global后面不接版本号只显示当前的全局版本</p>
<p>global后面也可以接多个版本，与local功能类似：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv global 2.7.6 3.3.3</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv versions</span></span><br><span class="line">  system</span><br><span class="line">* 2.7.6 (set by /Users/yyuu/.pyenv/version)</span><br><span class="line">* 3.3.3 (set by /Users/yyuu/.pyenv/version)</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python --version</span></span><br><span class="line">Python 2.7.6</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python2.7 --version</span></span><br><span class="line">Python 2.7.6</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python3.3 --version</span></span><br><span class="line">Python 3.3.3</span><br></pre></td></tr></table></figure>

<p><strong>pyenv shell</strong><br>通过设置环境变量<code>PYENV_VERSION</code>来指定当前shell环境的python版本，该版本会覆盖局部版本和全局版本。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv shell pypy-2.2.1</span><br></pre></td></tr></table></figure>
<p>相当于：<code>export PYENV_VERSION=pypy-2.2.1</code></p>
<p>取消当前shell环境设置的python版本：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv shell --unset</span><br></pre></td></tr></table></figure>
<p>同样可以为shell命令指定多个版本号：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv shell 2.7.6 3.3.3</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">pyenv versions</span></span><br><span class="line">  system</span><br><span class="line">* 2.7.6 (set by PYENV_VERSION environment variable)</span><br><span class="line">* 3.3.3 (set by PYENV_VERSION environment variable)</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python --version</span></span><br><span class="line">Python 2.7.6</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python2.7 --version</span></span><br><span class="line">Python 2.7.6</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">python3.3 --version</span></span><br><span class="line">Python 3.3.3</span><br></pre></td></tr></table></figure>

<h2 id="virtualenv使用"><a href="#virtualenv使用" class="headerlink" title="virtualenv使用"></a>virtualenv使用</h2><p>pyenv将virtualenv以一个插件的形式包含在pyenv中，pyenv的插件路径为：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">ls</span> ~/.pyenv/plugins/</span></span><br><span class="line">pyenv-doctor  pyenv-installer  pyenv-update  pyenv-virtualenv  pyenv-which-ext  python-build</span><br></pre></td></tr></table></figure>
<p>此处只简单介绍virtualenv用于python环境隔离的用法，更多virtualenv功能可以参见其官方文档。<br>virtualenv的使用方法为首先创建虚拟环境，然后激活虚拟环境。<br><strong>创建虚拟环境</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] &lt;virtualenv-name&gt;</span><br></pre></td></tr></table></figure>
<p>例如创建一个以python2.7.13为基础版，名为mypy2.7.13的虚拟环境：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv virtualenv 2.7.13 mypy2.7.13</span><br></pre></td></tr></table></figure>
<p>如果不指定版本，默认使用当前环境的python版本</p>
<p><strong>列出当前所有的虚拟环境</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv virtualenvs</span><br><span class="line">  2.7.13/envs/mypy2.7.13 (created from /home/blueyi/.pyenv/versions/2.7.13)</span><br><span class="line">  mypy2.7.13 (created from /home/blueyi/.pyenv/versions/2.7.13)</span><br></pre></td></tr></table></figure>
<p>也可以通过<code>pyenv versions</code>来查看：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv versions</span><br><span class="line">* system (set by /home/blueyi/.pyenv/version)</span><br><span class="line">  2.7.13</span><br><span class="line">  2.7.13/envs/mypy2.7.13</span><br><span class="line">  3.5.0</span><br><span class="line">  3.6.1</span><br><span class="line">  mypy2.7.13</span><br></pre></td></tr></table></figure>
<p>所以也可以为指定文件夹通过<code>pyenv local</code>来指定其版本为当前的虚拟环境</p>
<p><strong>激活指定的虚拟环境</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv activate mypy2.7.13</span><br></pre></td></tr></table></figure>
<p>激活之后，terminal的前面会有<code>(mypy2.7.13)</code>标识，此时通过pip安装包，以及相关的操作都只会修改该虚拟环境，对原python环境不会有任何影响。激活之后，直到手动退出该虚拟环境，或该shell之前都有效。而如果通过<code>pyenv local</code>指定局部环境为该虚拟环境的话，退出该文件夹即失效，再次进入又会自动生效。</p>
<p><strong>退出当前虚拟环境</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv deactivate</span><br></pre></td></tr></table></figure>

<p><strong>删除指定的虚拟环境</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pyenv uninstall mypy2.7.13</span><br></pre></td></tr></table></figure>

<p>pyenv配合其插件pyenv-virtualenv用于开发及测试，非常方便，再也不用怕环境被搞乱了。</p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p>1.<a target="_blank" rel="noopener" href="https://github.com/pyenv/pyenv">pyenv</a><br>2.<a target="_blank" rel="noopener" href="https://github.com/pyenv/pyenv/wiki">pyenv wiki</a><br>3.<a target="_blank" rel="noopener" href="https://github.com/pypa/virtualenv">virtualenv</a><br>4.<a target="_blank" rel="noopener" href="https://virtualenv.pypa.io">https://virtualenv.pypa.io</a></p>

    </div>

    
    
    
        

  <div class="followme">
    <p>Welcome to my other publishing channels</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Python/" rel="tag"># Python</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2017/03/02/vmware-shared-folder/" rel="prev" title="VMware配置linux guest中的shared folder">
      <i class="fa fa-chevron-left"></i> VMware配置linux guest中的shared folder
    </a></div>
      <div class="post-nav-item">
    <a href="/2017/03/09/linux-ssh-nopasswd/" rel="next" title="Linux下配置SSH免密访问">
      Linux下配置SSH免密访问 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E8%A3%85pyenv"><span class="nav-number">1.</span> <span class="nav-text">安装pyenv</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96"><span class="nav-number">1.1.</span> <span class="nav-text">安装依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pyenv%E5%AE%89%E8%A3%85"><span class="nav-number">1.2.</span> <span class="nav-text">pyenv安装</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pyenv%E4%BD%BF%E7%94%A8"><span class="nav-number">2.</span> <span class="nav-text">pyenv使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#virtualenv%E4%BD%BF%E7%94%A8"><span class="nav-number">3.</span> <span class="nav-text">virtualenv使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">4.</span> <span class="nav-text">参考</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="blueyi"
      src="/images/default_avatar.jpg">
  <p class="site-author-name" itemprop="name">blueyi</p>
  <div class="site-description" itemprop="description">心怀善意，虛怀若谷！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">104</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">68</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/blueyi" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;blueyi" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://maxwi.com/" title="http:&#x2F;&#x2F;maxwi.com" rel="noopener" target="_blank">Maxwi</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2014 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">blueyi</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">750k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">11:22</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '0f8243eb2c8b2207980f',
      clientSecret: 'd159633a33519d3b7a48b0ca729032f7d1f38a41',
      repo        : 'notes',
      owner       : 'blueyi',
      admin       : ['blueyi'],
      id          : '21509e476afbe80454b399e11668b24e',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

    </div>
</body>
</html>
